package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/catalog")
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    @GetMapping("viewHelp")
    public String viewHelp() {
        return "common/help";
    }

    @GetMapping("/view")
    public String view( Model model ){
        model.addAttribute( "account", null );
        return "catalog/main";
    }

    @GetMapping("/viewCategory")
    public String viewCategory( String categoryId, Model model ){
        if(categoryId!=null){
            Category category = catalogService.getCategory(categoryId);
            List<Product> productList = catalogService.getProductListByCategory( categoryId );
            model.addAttribute( "category", category );
            model.addAttribute( "productList", productList );
            return "catalog/category";
        }else{
            return "catalog/main";
        }
    }

    @GetMapping("/viewProduct")
    public String viewProduct( String productId, Model model ){
        if(productId!=null){
            Product product = catalogService.getProduct(productId);
            List<Item> itemList = catalogService.getItemListByProduct( productId );
            model.addAttribute( "product", product );
            model.addAttribute( "itemList", itemList );
            return "catalog/product";
        }else{
            return "catalog/main";
        }
    }

    @GetMapping("/viewItem")
    public String viewItem( String itemId, Model model ){
        if(itemId!=null){
            Item item = catalogService.getItem(itemId);
            model.addAttribute( "item", item );
            return "catalog/item";
        }else{
            return "catalog/main";
        }
    }

    @GetMapping("getAllProductList")
    @ResponseBody
    public String getAllProductList(){

        List<Product> productList = catalogService.getAllProductList();
        StringBuilder productListString = null;
        if ( !productList.isEmpty() ) {
            productListString = new StringBuilder( productList.get( 0 ).getName() + "" );
            if ( productList.size() > 1 ) {
                for ( int i = 1; i < productList.size(); i++ ) {
                    productListString.append( "," ).append( productList.get( i ).getName() ) ; //Product类的toString()默认返回对象的name字
                }
            }
        }

        return productListString.toString();
    }

    @PostMapping("/searchProduct")
    public String searchProduct( String keyword, HttpSession session ){

        List<Product> productList = catalogService.searchProductList(keyword);

        session.setAttribute("productList", productList);

        return "catalog/product_list";
    }

}
